package com.aruba.attendance.home.view

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Card
import androidx.compose.material3.Divider
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

/**
 * 设置弹框
 * Created by aruba on 2023/3/10.
 */
@Composable
fun SettingDialog(
    show: MutableState<Boolean>,
    items: Array<String>,
    modifier: Modifier = Modifier,
    selectCallback: ((index: Int) -> Unit)? = null
) {
    val dismiss: (index: Int) -> Unit = { index ->
        show.value = false
        selectCallback?.invoke(index)
    }

    AnimatedVisibility(visible = show.value, exit = fadeOut()) {
        Box(
            modifier = modifier
                .fillMaxSize()
                .clickable(
                    indication = null,
                    interactionSource = remember { MutableInteractionSource() },
                    onClick = { dismiss(-1) }
                )
        ) {
            Card(
                modifier = modifier
                    .align(Alignment.TopEnd)
                    .padding(end = 15.dp)
            ) {
                Column(
                    modifier = Modifier
                        .width(IntrinsicSize.Max)
                        .padding(5.dp),
                    horizontalAlignment = Alignment.End
                ) {

                    items.forEachIndexed { index, value ->
                        Text(
                            text = value,
                            modifier = Modifier.clickable {
                                dismiss(index)
                            }.padding(10.dp)
                        )

                        if (index != items.lastIndex) {
                            Divider(
                                color = LocalContentColor.current,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .padding(start = 5.dp, end = 5.dp)
                            )
                        }
                    }
                }
            }
        }
    }
}